/**
 * Copyright (c) 2016, 2024, All rights reserved.
 * Powered by [liangyafeng@duplicall.com] On 2024-03-22 15:29:50
 */
package com.yafengliang.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 *
 * <p>Project: SpringBoot - RecordingController
 * <p>@author liangyafeng@duplicall.com On 2024-03-22 15:29:50
 *
 * @Description
 */
@Controller
@Slf4j
public class RecordingController {

    @PostMapping(value = "upload")
    public String uploadAudio(@RequestParam("audio")MultipartFile audio){
        if (!audio.isEmpty()){
            try {
                //获取保存录音文件的目录
                String audioPath = "d:\\voice";
                //创建保存目录
                File dir = new File(audioPath);
                if (!dir.exists()){
                    dir.mkdirs();
                }
                //生成唯一的文件名
                String  fileName = System.currentTimeMillis()+".wav";

                //构建保存文件的完整路径
                String filePath = audioPath + File.separator + fileName;

                //保存录音文件
                audio.transferTo(new File(filePath));
                // 在这里可以根据需要进行处理，例如将文件路径保存到数据库或进行其他业务逻辑操作

                return "录音文件已上传：" + fileName;

            }catch (Exception e){
                log.error("上传录音文件失败:{}",e.toString());
                return "上传录音文件失败：" + e.getMessage();
            }
        }
        return "录音文件为空。";
    }

}
